<?php

namespace gerador\Model;

class GeradorUniweb extends GeradorAppModel {

    public $database = "uniweb";

    public function getColunas($tabela, $where=null){
        (!empty($where)) ? $where = "and $where" : false;
        return $this->select("column_name, nullable")->from("user_tab_columns")->where("table_name = UPPER(:t) $where")->execPrepQuery(["t"=>$tabela]);
    }

    public function show_tables($schema){
        return $this->select("owner, table_name")->from("all_tables")->where("owner=upper(:schema)")->order("owner, table_name")->execPrepQuery(["schema"=>$schema]);
    }

    public function containsColumn($tb, $col){
        return $this->countAll()->from("user_tab_columns")->where("table_name = UPPER(:t) and column_name = UPPER(:c)")->execPrepCount(["t"=>$tb, "c"=>$col]);
    }

    public function addController($controller, $modulo_id){
        $rs = $this->select("id")->from("controllers")->where("descricao = :c and modulo_id = :m")->execPrepQuery(array("c" => $controller, "m" => $modulo_id));
        if(empty($rs[0]->ID)){
            $data["action_type"] = "save";
            $data["modulo_id"] = $modulo_id;
            $data["descricao"] = $controller;
            $data["status"] = 1;
            $this->AutoExecute($data, "controllers");
            return $this->db->genID;
        }else{
            return $rs[0]->ID;
        }
    }

    public function setPermissions($modulo_id, $controller_id, $actions){
        $rs = $this->select("id")->from("grupos_usuarios")->where("modulo_id = :m")->execPrepQuery(["m" => $modulo_id]);
        $grupos_usuarios_id = $rs[0]->ID;
//        $actions = ["index", "list", "add", "edit", "view", "del", "status"];
        foreach($actions as $action){
            $total = $this->countAll()->from("permissoes p")->where("p.grupos_usuarios_id = :g and p.modulo_id = :m and p.controllers_id = :c and p.action = :a")->execPrepCount(["g" => $grupos_usuarios_id, "m" => $modulo_id, "c" => $controller_id, "a" => $action]);
            $data["status"] = 1;
            if($total == 0){
                $data["grupos_usuarios_id"] = $grupos_usuarios_id;
                $data["modulo_id"] = $modulo_id;
                $data["controllers_id"] = $controller_id;
                $data["action"] = $action;
                $data["action_type"] = "save";
                $where = false;
            }else{
                $data["action_type"] = "update";
                $where = "modulo_id = $modulo_id and controllers_id = $controller_id and action = '$action'";
            }
            $this->AutoExecute($data, "permissoes", $where);
        }
    }

    public function getGruposUsuarios($modulo){
        $rs = $this->select()->from("grupos_usuarios")->where("modulo_id <> :id")->execPrepQuery(["id"=>$modulo]);
        if(count($rs) > 0){
            return $rs;
        }else{
            return false;
        }
    }

    public function gerarMenu($modulo_id, $controller_id, $controller){
        $total = $this->countAll()->from("menus")->where("modulo_id = :m and controllers_id = :c")->execPrepCount(["m"=>$modulo_id, "c"=>$controller_id]);
        if($total == 0){
            $data["action_type"] = "save";
            $data["modulo_id"] = $modulo_id;
            $data["controllers_id"] = $controller_id;
            $data["titulo"] = $controller;
            $data["status"] = 1;
            $this->AutoExecute($data, "menus");
        }
    }

}